import React from 'react'
import {useState} from "react"
import axios from "axios"
import { Toast } from 'react-vant';
function Login(props) {
   let [val,setVal] = useState("")
   let [paw,setPaw] = useState('')
   const login = ()=>{
        console.log(val,paw);
        // 前端路由得名字和后端接口得名字一定不能一样，否则会冲突
        axios.get("/login1",{params:{username:val,password:paw}}).then(res=>{
            console.log(res.data);
            if ( res.data.data == "1") {
                // 跳转路由
                props.history.push({ // 跳转路由
                    pathname:"/my"
                })
                window.localStorage.setItem("islogin","true")
                Toast.info('登陆成功')
            } else {
                // 提示登陆失败
                Toast.info('用户名或者密码错误')

            }
        })
   }
   const setUsername = (e)=>{
         setVal(e.target.value)
   }
   const setPaw1 = (e)=>{
        setPaw(e.target.value)
   }
  return (
    <div>
        
            用户名：<input type="text" value={val} onChange={setUsername}/>
            <br />
       
          
              密码：  <input type="password" value={paw} onChange={setPaw1}/>
       <br />   
       
        <button onClick={login}>登陆</button>
    </div>
  )
}

export default Login